package com.kbt.listener;

import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

/**
 * @author zhgk
 * @package: com.kbt.listener
 * @TODO: 点击复选框触发的事件, 用于计算总金额
 * @data: 2015/8/13 9:28
 * @version: V1.0
 */
public class ClickCheckGoodListener implements View.OnClickListener {

    private TextView totalFee;
    private float goodPrice;
    private TextView goodCount;

    public ClickCheckGoodListener(TextView totalFee, float goodPrice, TextView goodCount) {
        this.totalFee = totalFee;
        this.goodPrice = goodPrice;
        this.goodCount = goodCount;
    }

    @Override
    public void onClick(View v) {
        String goodCountStr = goodCount.getText().toString().trim();
        int count = goodCountStr.length() == 0 ? 0 : Integer.parseInt(goodCountStr.substring(2));
        String totalFeeStr = totalFee.getText().toString().trim();
        float total = totalFeeStr.length() == 0 ? 0.0f : Float.parseFloat(totalFeeStr);
        CheckBox cx = (CheckBox) v;
        boolean checked = ((CheckBox) v).isChecked();
        if (checked) {
            //增加金额
            totalFee.setText(String.valueOf(total + goodPrice * count));

        } else {
            //减少金额
            totalFee.setText(String.valueOf(total - goodPrice * count));
        }

    }
}
